package com.bjtu.laorenyouhao.yuangong.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjtu.common.valid.group.AddGroupp;
import com.bjtu.common.valid.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjtu.laorenyouhao.yuangong.entity.YuangongInfoEntity;
import com.bjtu.laorenyouhao.yuangong.service.YuangongInfoService;
import com.bjtu.common.utils.PageUtils;
import com.bjtu.common.utils.R;



/**
 * 员工信息表
 *
 * @author yangyalong
 * @email yangyalong@gmail.com
 * @date 2021-07-08 21:03:25
 */
@RestController
@RequestMapping("yuangong/yuangonginfo")
public class YuangongInfoController {
    @Autowired
    private YuangongInfoService yuangongInfoService;

    @RequestMapping("/test")
    public R test(@RequestBody Map data){

        System.out.println("过来了" + data);
        return R.ok().put("result","拿到了");
    }

    /**
     * 根据名字获得id
     */
    @RequestMapping("/getId")
    public R getYuangongId(@RequestParam String name){
        YuangongInfoEntity yuangong = yuangongInfoService.getOne(new QueryWrapper<YuangongInfoEntity>().eq("name", name));
        return R.ok().put("yuangongId",yuangong.getId());
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = yuangongInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		YuangongInfoEntity yuangongInfo = yuangongInfoService.getById(id);

        return R.ok().put("yuangongInfo", yuangongInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody YuangongInfoEntity yuangongInfo){
        System.out.println(yuangongInfo);
        yuangongInfo.setHireDate(new Date());
        yuangongInfo.setIsRemove(1);
        yuangongInfo.setProfilePhoto("https://laorenyouhao.oss-cn-beijing.aliyuncs.com/" + yuangongInfo.getProfilePhoto());
        yuangongInfo.setImgsetDir(yuangongInfo.getImgsetDir());
		yuangongInfoService.save(yuangongInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody YuangongInfoEntity yuangongInfo){
		yuangongInfoService.updateById(yuangongInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		yuangongInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
